home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / ProgramD2.iso / Borland / Borland C++ V5.02 / SCRIB3.PAK / SCRIBVW.CPP < prev    next >
C/C++ Source or Header  |  1997-05-06  |  5KB  |  188 lines

  1. // ScribVw.cpp : implementation of the CScribbleView class
  2. //
  3. // This is a part of the Microsoft Foundation Classes C++ library.
  4. // Copyright (C) 1992-1995 Microsoft Corporation
  5. // All rights reserved.
  6. //
  7. // This source code is only intended as a supplement to the
  8. // Microsoft Foundation Classes Reference and related
  9. // electronic documentation provided with the library.
  10. // See these sources for detailed information regarding the
  11. // Microsoft Foundation Classes product.
  12.  
  13. #include "stdafx.h"
  14. #include "Scribble.h"
  15.  
  16. #include "ScribDoc.h"
  17. #include "ScribVw.h"
  18.  
  19. #ifdef _DEBUG
  20. #define new DEBUG_NEW
  21. #undef THIS_FILE
  22. static char THIS_FILE[] = __FILE__;
  23. #endif
  24.  
  25. /////////////////////////////////////////////////////////////////////////////
  26. // CScribbleView
  27.  
  28. IMPLEMENT_DYNCREATE(CScribbleView, CView)
  29.  
  30. BEGIN_MESSAGE_MAP(CScribbleView, CView)
  31.     //{{AFX_MSG_MAP(CScribbleView)
  32.     ON_WM_LBUTTONDOWN()
  33.     ON_WM_LBUTTONUP()
  34.     ON_WM_MOUSEMOVE()
  35.     //}}AFX_MSG_MAP
  36.     // Standard printing commands
  37.     ON_COMMAND(ID_FILE_PRINT, CView::OnFilePrint)
  38.     ON_COMMAND(ID_FILE_PRINT_DIRECT, CView::OnFilePrint)
  39.     ON_COMMAND(ID_FILE_PRINT_PREVIEW, CView::OnFilePrintPreview)
  40. END_MESSAGE_MAP()
  41.  
  42. /////////////////////////////////////////////////////////////////////////////
  43. // CScribbleView construction/destruction
  44.  
  45. CScribbleView::CScribbleView()
  46. {
  47.     // TODO: add construction code here
  48.  
  49. }
  50.  
  51. CScribbleView::~CScribbleView()
  52. {
  53. }
  54.  
  55. BOOL CScribbleView::PreCreateWindow(CREATESTRUCT& cs)
  56. {
  57.     // TODO: Modify the Window class or styles here by modifying
  58.     //  the CREATESTRUCT cs
  59.  
  60.     return CView::PreCreateWindow(cs);
  61. }
  62.  
  63. /////////////////////////////////////////////////////////////////////////////
  64. // CScribbleView drawing
  65.  
  66. void CScribbleView::OnDraw(CDC* pDC)
  67. {
  68.     CScribbleDoc* pDoc = GetDocument();
  69.     ASSERT_VALID(pDoc);
  70.  
  71.     // The view delegates the drawing of individual strokes to
  72.     // CStroke::DrawStroke().
  73.     CTypedPtrList<CObList,CStroke*>& strokeList = pDoc->m_strokeList;
  74.     POSITION pos = strokeList.GetHeadPosition();
  75.     while (pos != NULL)
  76.     {
  77.         CStroke* pStroke = strokeList.GetNext(pos);
  78.         pStroke->DrawStroke(pDC);
  79.     }
  80. }
  81.  
  82. /////////////////////////////////////////////////////////////////////////////
  83. // CScribbleView printing
  84.  
  85. BOOL CScribbleView::OnPreparePrinting(CPrintInfo* pInfo)
  86. {
  87.     // default preparation
  88.     return DoPreparePrinting(pInfo);
  89. }
  90.  
  91. void CScribbleView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
  92. {
  93.     // TODO: add extra initialization before printing
  94. }
  95.  
  96. void CScribbleView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
  97. {
  98.     // TODO: add cleanup after printing
  99. }
  100.  
  101. /////////////////////////////////////////////////////////////////////////////
  102. // CScribbleView diagnostics
  103.  
  104. #ifdef _DEBUG
  105. void CScribbleView::AssertValid() const
  106. {
  107.     CView::AssertValid();
  108. }
  109.  
  110. void CScribbleView::Dump(CDumpContext& dc) const
  111. {
  112.     CView::Dump(dc);
  113. }
  114.  
  115. CScribbleDoc* CScribbleView::GetDocument() // non-debug version is inline
  116. {
  117.     ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CScribbleDoc)));
  118.     return (CScribbleDoc*)m_pDocument;
  119. }
  120. #endif //_DEBUG
  121.  
  122. /////////////////////////////////////////////////////////////////////////////
  123. // CScribbleView message handlers
  124.  
  125. void CScribbleView::OnLButtonDown(UINT, CPoint point) 
  126. {
  127.     // Pressing the mouse button in the view window starts a new stroke
  128.  
  129.     m_pStrokeCur = GetDocument()->NewStroke();
  130.     // Add first point to the new stroke
  131.     m_pStrokeCur->m_pointArray.Add(point);
  132.  
  133.     SetCapture();       // Capture the mouse until button up.
  134.     m_ptPrev = point;   // Serves as the MoveTo() anchor point for the
  135.                         // LineTo() the next point, as the user drags the
  136.                         // mouse.
  137.  
  138.     return;
  139. }
  140.  
  141. void CScribbleView::OnLButtonUp(UINT, CPoint point) 
  142. {
  143.     // Mouse button up is interesting in the Scribble application
  144.     // only if the user is currently drawing a new stroke by dragging
  145.     // the captured mouse.
  146.  
  147.     if (GetCapture() != this)
  148.         return; // If this window (view) didn't capture the mouse,
  149.                 // then the user isn't drawing in this window.
  150.  
  151.     CScribbleDoc* pDoc = GetDocument();
  152.  
  153.     CClientDC dc(this);
  154.  
  155.     CPen* pOldPen = dc.SelectObject(pDoc->GetCurrentPen());
  156.     dc.MoveTo(m_ptPrev);
  157.     dc.LineTo(point);
  158.     dc.SelectObject(pOldPen);
  159.     m_pStrokeCur->m_pointArray.Add(point);
  160.  
  161.     ReleaseCapture();   // Release the mouse capture established at
  162.                         // the beginning of the mouse drag.
  163.     return;
  164. }
  165.  
  166. void CScribbleView::OnMouseMove(UINT, CPoint point) 
  167. {
  168.     // Mouse movement is interesting in the Scribble application
  169.     // only if the user is currently drawing a new stroke by dragging
  170.     // the captured mouse.
  171.  
  172.     if (GetCapture() != this)
  173.         return; // If this window (view) didn't capture the mouse,
  174.                 // then the user isn't drawing in this window.
  175.  
  176.     CClientDC dc(this);
  177.     m_pStrokeCur->m_pointArray.Add(point);
  178.  
  179.     // Draw a line from the previous detected point in the mouse
  180.     // drag to the current point.
  181.     CPen* pOldPen = dc.SelectObject(GetDocument()->GetCurrentPen());
  182.     dc.MoveTo(m_ptPrev);
  183.     dc.LineTo(point);
  184.     dc.SelectObject(pOldPen);
  185.     m_ptPrev = point;
  186.     return;
  187. }
  188.